from windoh.control import Control
from windoh.controls.slider import Slider


class RGBSliders(Control):
    slider_class = Slider

    def __init__(self, color=None, **kwargs):
        Control.__init__(self, **kwargs)

        self._color = color or [0.0, 0.0, 0.0]

        r = self.slider_class(direction="horiz", value=color[0], name='r', 
                                left=1, top=1, right=10, height=15)
        g = self.slider_class(direction="horiz", value=color[1], name='g', 
                                left=1, top=21, right=10, height=15)
        b = self.slider_class(direction="horiz", value=color[2], name='b', 
                                left=1, top=41, right=10, height=15)

        self.extend([r, g, b])

        r.push_handlers(on_value_change=self.on_r_change)
        g.push_handlers(on_value_change=self.on_g_change)
        b.push_handlers(on_value_change=self.on_b_change)

        self._r, self._g, self._b = r, g, b

        self._children_proxy = ()

    def __getstate__(self):
        d = Control.__getstate__(self)
        d['color'] = self._color
        return d

    def _get_color(self):
        return self._color
    def _set_color(self, c):
        self.dispatch_event('on_value_change', c)
        if self._r.value != self._color[0]:
            self._r.value = self._color[0]
        if self._g.value != self._color[1]:
            self._g.value = self._color[1]
        if self._b.value != self._color[2]:
            self._b.value = self._color[2]

    color = property(_get_color, _set_color)

    def on_r_change(self, v):
        self.dispatch_event('on_value_change', [v, self._color[1], self._color[2]])

    def on_g_change(self, v):
        self.dispatch_event('on_value_change', [self._color[0], v, self._color[2]])

    def on_b_change(self, v):
        self.dispatch_event('on_value_change', [self._color[0], self._color[1], v])

    def on_value_change(self, v):
        self._color = v

RGBSliders.register_event_type('on_value_change')


